/**
 * @Author liangzai
 * @Description:
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;

public class PlaneWar extends JFrame {
    private GamePanel gamePanel;

    public PlaneWar() {
        setTitle("Java飞机大战");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);

        gamePanel = new GamePanel();
        add(gamePanel);

        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {
        new PlaneWar();
    }
}

class GamePanel extends JPanel implements ActionListener, KeyListener {
    public static final int WIDTH = 400;
    public static final int HEIGHT = 600;
    public static final int PLAYER_SPEED = 5;
    public static final int ENEMY_SPEED = 2;
    public static final int BULLET_SPEED = 7;

    private Player player;
    private ArrayList<Enemy> enemies;
    private ArrayList<Bullet> bullets;
    private Random random;
    private Timer timer;
    private int score;
    private boolean gameOver;

    public GamePanel() {
        setPreferredSize(new Dimension(WIDTH, HEIGHT));
        setBackground(Color.BLACK);
        setFocusable(true);
        addKeyListener(this);

        player = new Player(WIDTH / 2 - 25, HEIGHT - 100, 50, 50);
        enemies = new ArrayList<>();
        bullets = new ArrayList<>();
        random = new Random();
        score = 0;
        gameOver = false;

        timer = new Timer(16, this); // 约60FPS
        timer.start();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        // 绘制玩家飞机
        player.draw(g);

        // 绘制敌机
        for (Enemy enemy : enemies) {
            enemy.draw(g);
        }

        // 绘制子弹
        for (Bullet bullet : bullets) {
            bullet.draw(g);
        }

        // 绘制分数
        g.setColor(Color.WHITE);
        g.setFont(new Font("Arial", Font.BOLD, 20));
        g.drawString("score:" + score, 10, 20);

        // 游戏结束显示
        if (gameOver) {
            g.setColor(Color.yellow);
            g.setFont(new Font("Arial", Font.BOLD, 40));
            g.drawString("Game Over!", WIDTH / 2 - 100, HEIGHT / 2);
            g.setFont(new Font("Arial", Font.BOLD, 20));
            g.drawString("Final Score: " + score, WIDTH / 2 - 60, HEIGHT / 2 + 40);
            g.drawString("    Resume to R", WIDTH / 2 - 70, HEIGHT / 2 + 80);
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (!gameOver) {
            // 随机生成敌机
            if (random.nextInt(50) == 0) {
                enemies.add(new Enemy(random.nextInt(WIDTH - 50), -50, 50, 50));
            }

            // 移动玩家子弹
            for (int i = 0; i < bullets.size(); i++) {
                Bullet bullet = bullets.get(i);
                bullet.move();

                // 移除超出屏幕的子弹
                if (bullet.getY() < 0) {
                    bullets.remove(i);
                    i--;
                }
            }

            // 移动敌机并检测碰撞
            for (int i = 0; i < enemies.size(); i++) {
                Enemy enemy = enemies.get(i);
                enemy.move();

                // 检测敌机与玩家碰撞
                if (player.getBounds().intersects(enemy.getBounds())) {
                    gameOver = true;
                    break;
                }

                // 检测敌机与子弹碰撞
                for (int j = 0; j < bullets.size(); j++) {
                    Bullet bullet = bullets.get(j);
                    if (enemy.getBounds().intersects(bullet.getBounds())) {
                        enemies.remove(i);
                        bullets.remove(j);
                        score += 10;
                        i--;
                        break;
                    }
                }

                // 移除超出屏幕的敌机
                if (enemy.getY() > HEIGHT) {
                    enemies.remove(i);
                    i--;
                }
            }
        }

        repaint();
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (!gameOver) {
            switch (e.getKeyCode()) {
                case KeyEvent.VK_LEFT:
                    player.move(-PLAYER_SPEED, 0);
                    break;
                case KeyEvent.VK_RIGHT:
                    player.move(PLAYER_SPEED, 0);
                    break;
                case KeyEvent.VK_UP:
                    player.move(0, -PLAYER_SPEED);
                    break;
                case KeyEvent.VK_DOWN:
                    player.move(0, PLAYER_SPEED);
                    break;
                case KeyEvent.VK_SPACE:
                    bullets.add(new Bullet(player.getX() + player.getWidth() / 2 - 2, player.getY(), 4, 10));
                    break;
            }
        } else {
            if (e.getKeyCode() == KeyEvent.VK_R) {
                // 重新开始游戏
                player = new Player(WIDTH / 2 - 25, HEIGHT - 100, 50, 50);
                enemies.clear();
                bullets.clear();
                score = 0;
                gameOver = false;
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {}

    @Override
    public void keyTyped(KeyEvent e) {}
}

class GameObject {
    protected int x, y, width, height;
    protected Color color;

    public GameObject(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }

    public Rectangle getBounds() {
        return new Rectangle(x, y, width, height);
    }

    public int getX() { return x; }
    public int getY() { return y; }
    public int getWidth() { return width; }
    public int getHeight() { return height; }

    public void move(int dx, int dy) {
        x += dx;
        y += dy;
    }
}

class Player extends GameObject {
    public Player(int x, int y, int width, int height) {
        super(x, y, width, height);
        color = Color.BLUE;
    }

    public void draw(Graphics g) {
        g.setColor(color);
        g.fillRect(x, y, width, height);

        // 简单的飞机形状
        g.setColor(Color.WHITE);
        g.fillRect(x + width/2 - 5, y - 10, 10, 10); // 机头
        g.fillRect(x, y + height/2 - 5, width, 10); // 机翼
    }

    @Override
    public void move(int dx, int dy) {
        // 限制玩家移动范围
        if (x + dx >= 0 && x + dx + width <= GamePanel.WIDTH) {
            x += dx*3;
        }
        if (y + dy >= 0 && y + dy + height <= GamePanel.HEIGHT) {
            y += dy*3;
        }
    }
}

class Enemy extends GameObject {
    public Enemy(int x, int y, int width, int height) {
        super(x, y, width, height);
        color = Color.RED;
    }

    public void draw(Graphics g) {
        g.setColor(color);
        g.fillRect(x, y, width, height);

        // 简单的敌机形状
        g.setColor(Color.BLACK);
        g.fillRect(x + width/2 - 5, y + height, 10, 10); // 尾部
        g.fillRect(x, y + height/2 - 5, width, 10); // 机翼
    }

    public void move() {
        y += GamePanel.ENEMY_SPEED;
    }
}

class Bullet extends GameObject {
    public Bullet(int x, int y, int width, int height) {
        super(x, y, width, height);
        color = Color.YELLOW;
    }

    public void draw(Graphics g) {
        g.setColor(color);
        g.fillRect(x, y, width, height);
    }

    public void move() {
        y -= GamePanel.BULLET_SPEED;
    }
}
